package com.hangzhou.xyj.ui.fragment;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View.OnClickListener;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.model.ActionModel;
import com.hangzhou.xyj.model.ActionPageInfoModel;
import com.hangzhou.xyj.model.BannerModel;
import com.hangzhou.xyj.model.type.EBannerType;
import com.hangzhou.xyj.net.handler.GetBannerHandler;
import com.hangzhou.xyj.net.handler.GetListOfInfoHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.GetBannerReq;
import com.hangzhou.xyj.net.request.GetListOfInfoReq;
import com.hangzhou.xyj.ui.adapter.HotspotAdapter;
import com.hangzhou.xyj.ui.adapter.base.BaseLoadingAdapter;
import com.hangzhou.xyj.ui.fragment.base.BaseFragment;
import com.hangzhou.xyj.ui.widget.DividerItemDecoration;

import java.util.List;

/**
 * 热点
 *
 * @author neo.duan
 */
public class HotspotFragment extends BaseFragment implements OnClickListener {
    private RecyclerView mRecyclerView;
    private List<ActionModel> mDataList;//服务器返回的数据,全局变量用m开头
    private HotspotAdapter mAdapter;
    private int mPageSize = 10;//每页条目数

    @Override
    public void initTop() {
        enableTop(false);
        enableRefresh(true);
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.fragment_hotspot);
    }

    @Override
    public void initViews() {
        mRecyclerView = findView(R.id.rv_hotspot);
        //设置水平布局
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        // 设置item动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        //设置水平分割线
        mRecyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST
                , R.drawable.line_divider));
        mRecyclerView.setAdapter(mAdapter = new HotspotAdapter(mRecyclerView, null));
    }

    @Override
    public void initDatas() {
        mAdapter.setmPageSize(mPageSize);
        getBanner();
    }

    @Override
    public void initListeners() {
        mAdapter.setOnLoadingListener(new BaseLoadingAdapter.OnLoadingListener() {
            @Override
            public void loading() {
                mRecyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        int currentPage = mAdapter.getCurrentPage(mPageSize);
                        getListInfo(currentPage);
                    }
                }, 500);
            }
        });
    }

    /**
     * 获取banner条数据
     */
    private void getBanner() {
        GetBannerReq req = new GetBannerReq(EBannerType.STAGE);
        HttpLoader.getInstance().sendRequest(new GetBannerHandler(req, new MyHttpResponseListener() {
            @Override
            public void onStart() {
                //不要loading框
            }

            @Override
            public void onSuccess(Object model) {
                hideParentLoadingDialog();
//				super.onSuccess(model); 不需要关闭Loading对话框，所以注释掉
                //更新banner数据
                List<BannerModel> bannerList = (List<BannerModel>) model;
                mAdapter.updateBanner(bannerList);
                //获取banner成功，再获取列表
                getListInfo(1);
            }
        }, 0));
    }

    private void getListInfo(final int currentPage) {
        //请求服务器
        GetListOfInfoReq req = new GetListOfInfoReq(GetListOfInfoReq.TYPE_HOT_SPOT, 0, currentPage, mPageSize);
        HttpLoader.getInstance().sendRequest(new GetListOfInfoHandler(req, new MyHttpResponseListener() {
            @Override
            public void onStart() {
                //不要loading框
            }
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);//这段代码一定要调用
                mDataList = ((ActionPageInfoModel) model).getActionList();
                if (currentPage == 1) {
//                    mAdapter.setmListSize(mDataList.size() + 1);
                    mAdapter.load(mDataList);
                } else {
//                    mAdapter.setmListSize(mDataList.size() + 1);
                    mAdapter.update(mDataList);
                }
            }

            @Override
            public void onFail(String msg) {
                super.onFail(msg);
                mAdapter.setLoadingNoMore();//加载失败的时候
            }

            @Override
            public void onError() {
                super.onError();
                mAdapter.setLoadingNoMore();//加载失败的时候
            }

        }, 0));
    }

    @Override
    public void onClick(View v) {

    }
}
